<?php

namespace App\Http\Controllers;

use App\Model\User;
use Encore\Admin\Grid\Model;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Commodity_classify;

class IndexController extends Controller
{
    /**
     * 方法 : index
     * @User wuqin
     * @DateTime 2018-02-27 21:20
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
//        return error('/home','注册失败',3);

        $open_all = session("wechat.oauth_user.default")->toArray();
        $original = $open_all["original"];

        $user = new User;

        if (empty($res = $user->where('openid', '=', $original['openid'])->first())) {
            if( !$user->add_user($original))
                return $this->error_url('/','注册失败',3);
        } else {
            if( !$res->update_user($original))
                return $this->error_url('/','更新失败',3);
        }

        $classify = new Commodity_classify;

        $data = [
            'user' => $open_all,
            'classify' => $classify->get_classify()
        ];
        $data["commodity"] = $this->index_data();
        $data["banner"] = $this->index_banner();
        return view('Home.index', $data);
    }
}
